﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;

namespace Engine.Helpers
{
    public static class BoundingBoxExtensions
    {
        private static Vector3[] _bboxCorners = new Vector3[8];
        private static Vector3[] _transformedCorners = new Vector3[8];
        public static BoundingBox Transform(this BoundingBox bbox, Matrix transform)
        {
            bbox.GetCorners(_bboxCorners);
            Vector3.Transform(_bboxCorners, ref transform, _transformedCorners);

            Vector3 min = _transformedCorners[0];
            Vector3 max = min;

            for (int i = 1; i < 8; ++i)
            {
                Vector3 corner = _transformedCorners[i];
                min = Vector3.Min(min, corner);
                max = Vector3.Max(max, corner);
            }

            return new BoundingBox(min, max);
        }
    }
}
